<?php

namespace App\Http\Controllers\Api;

use App\Models\User;

class ShippingController extends Controller
{

    //获取token
    public function getAccessToken()
    {
        $wechat = new WechatController();
        $token = $wechat->getAccessToken();
        return $token;
    }
//获取发货订单信息
    public function getWxSendOrderStatus($transaction_id)
    {

        $token = $this->getAccessToken();

        $url = "https://api.weixin.qq.com/wxa/sec/order/get_order?access_token=" . $token;

        $data = [
            'transaction_id' => $transaction_id
        ];
        $data = json_encode($data, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);

        $result = $this->curl_post($url, $data);

        $result = json_decode($result, true);

        return $result;

    }



//设置微信发货后，消息跳转地址，不设置为默认
    public function set_jump_path()
    {

        $token = $this->getAccessToken();

        $url = "https://api.weixin.qq.com/wxa/sec/order/set_msg_jump_path?access_token=" . $token;

        $data = [
            'path' => 'pages/order/index'
        ];
        $data = json_encode($data, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);

        $this->curl_post($url, $data);

    }
//发货 物流15天自动确认，虚拟商品隔天自动确认
    public function sendDelivery($order)
    {

        // $this->set_jump_path();

        $token = $this->getAccessToken();

        if ($order['type'] == 1) {
            $desc = "开蚌订单";
        }

        if ($order['type'] == 2) {
            $desc = "饰品订单";
        }

        if ($order['type'] == 3) {
            $desc = "成品订单";
        }

        $openid = User::where('id',$order['user_id'])->value('openid');
        $data = [
            'order_key' => [

                'order_number_type' => 1,
                'mchid'=>'1699304091',
                'out_trade_no' => $order['order_sn']
            ],
            'logistics_type' => 4,//物流模式，发货方式枚举值：1、实体物流配送采用快递公司进行实体物流配送形式 2、同城配送 3、虚拟商品，虚拟商品，例如话费充值，点卡等，无实体配送形式 4、用户自提
            'delivery_mode' => 1,
            'shipping_list' => [
                [

                    'item_desc' => $desc,
                ]
            ],
            'upload_time' => date('Y-m-d\TH:i:sP', time()),
            'payer' => [
                'openid' => $openid
            ]
        ];


        $urlss = "https://api.weixin.qq.com/wxa/sec/order/upload_shipping_info?access_token=" . $token;
        $data = json_encode($data, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);
        $results = $this->curl_post($data,$urlss);

        $results = json_decode($results, true);
        file_put_contents("/www/wwwroot/lanbang/pay.log",date("Y-m-d H:i:s").'-order-'.json_encode($order)."\r\n",FILE_APPEND);
        file_put_contents("/www/wwwroot/lanbang/pay.log",date("Y-m-d H:i:s").'-delivery-'.json_encode($results)."\r\n",FILE_APPEND);
//        if ($results['errcode'] == 0) {
//            return $results;
//        } else {
//            return $results;
//        }

    }

    public function curl_post($data,$url){

        $curl = curl_init();

        curl_setopt_array($curl, array(
            CURLOPT_URL => $url,
            CURLOPT_RETURNTRANSFER => true,
            CURLOPT_ENCODING => '',
            CURLOPT_MAXREDIRS => 10,
            CURLOPT_TIMEOUT => 0,
            CURLOPT_FOLLOWLOCATION => true,
            CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
            CURLOPT_CUSTOMREQUEST => 'POST',
            CURLOPT_POSTFIELDS =>$data,
            CURLOPT_HTTPHEADER => array(
                'Content-Type: application/json'
            ),
        ));
        curl_setopt($curl,CURLOPT_SSL_VERIFYPEER,false);
        $response = curl_exec($curl);

        curl_close($curl);

        return $response;

    }



}
